<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

define('NO_NEED_AUTH',TRUE);

class Getstatus extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->model('queue_model');
    }



    /**
     * error
     *
     * @access public
     * @param string $msg 错误失败的消息 [Must]
     * @param array $data 错误失败的相关错误数据 [Must]
     * @return string $mixed
     */
    public function error($msg,$data = '') {
        $rs['error'] = $msg;
        $rs['data'] = $data;

        header('Content-type: application/json');
        echo json_encode($rs);
    }

    /**
     * success
     *
     * @access public
     * @param array $data xxxxx [Must]
     * @return string $mixed
     */
    public function success($data) {
        $rs['error'] = '';
        $rs['data'] = $data;

        header('Content-type: application/json');
        echo json_encode($rs);
    }



    //微博 10分钟内 同步 数量
    function sync_num(){

        try {

            $curr_date = date('Y-m-d', strtotime('-10 mins'));
            $curr_time = substr(date('m-d H:i', strtotime('-10 mins')), 0, -1);

            //echo("cat /home/www/crawler_admin_sync/application/logs/article_".$curr_date.".log | grep '".$curr_time."'
            // | wc -l \n");
            //$cmd = popen("cat ../../logs/article_2017-09-27.log | grep '09-27 15:44' | wc -l", 'r');
            $cmd = popen("cat /home/data1/yijian_weibo_task/application/logs/article_" . $curr_date . ".log | grep '" . $curr_time
                . "' | wc -l",
                'r');
            $current_num = fgets($cmd, 512);
            $current_num = str_replace(PHP_EOL,"",$current_num);

            fclose($cmd);

            $this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }


    //微博 当天 即时 已同步 数量
    function sync_num_today(){

        try {

            $curr_date = date('Y-m-d', time());

            //echo("cat /home/www/crawler_admin_sync/application/logs/article_".$curr_date.".log | grep '".$curr_time."'
            // | wc -l \n");
            //$cmd = popen("cat ../../logs/article_2017-09-27.log | grep '09-27 15:44' | wc -l", 'r');
            $cmd = popen("cat /home/data1/yijian_weibo_task/application/logs/article_" . $curr_date . ".log | grep '" . $curr_date
                . "' | wc -l",
                'r');
            $current_num = fgets($cmd, 512);
            $current_num = str_replace(PHP_EOL,"",$current_num);

            fclose($cmd);

            $this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }


    //微博 指定日期(默认当天) 指定时间 已同步 数量
    function sync_num_today_time(){

        $date_str = !empty($_GET['date']) ? trim($_GET['date']) : date('Y-m-d', time());
        $time_str = !empty($_GET['time']) ? trim($_GET['time']) : '';

        try {


            //echo("cat /home/www/crawler_admin_sync/application/logs/article_".$curr_date.".log | grep '".$curr_time."'
            // | wc -l \n");
            //$cmd = popen("cat ../../logs/article_2017-09-27.log | grep '09-27 15:44' | wc -l", 'r');
            $cmd = popen("cat /home/data1/yijian_weibo_task/application/logs/article_" . $date_str . ".log | grep '" . $date_str." ". $time_str . "' | wc -l",
                'r');
            $current_num = fgets($cmd, 512);
            $current_num = str_replace(PHP_EOL,"",$current_num);

            fclose($cmd);

            $this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }


    //微博抓取 调试 yijian_weibo_task 指定的 方法
    function weibo_func(){

        $control = !empty($_GET['control']) ? trim($_GET['control']) : 'guanzhu';
        $func = !empty($_GET['func']) ? trim($_GET['func']) : 'test';

        try {


            //echo("cat /home/www/crawler_admin_sync/application/logs/article_".$curr_date.".log | grep '".$curr_time."'
            // | wc -l \n");
            //$cmd = popen("cat ../../logs/article_2017-09-27.log | grep '09-27 15:44' | wc -l", 'r');
            $cmd = popen("/usr/local/php/bin/php /home/data1/yijian_weibo_task/index.php crontab ". $control ." ".
                $func,
                'r');
            $current_num = fgets($cmd, 512);
            $current_num = str_replace(PHP_EOL,"",$current_num);

            fclose($cmd);

            header('Content-type: application/text');
            echo($current_num);

            //$this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }




	//微博 博主 堆积数量
    function user_queue_num(){

        try {

            $current_num = $this->queue_model->get_v4_crawl_user_task_num();

            $this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }

    //微博 转发 堆积数量
    function zhuanfa_queue_num(){

        try {

            $current_num = $this->queue_model->get_zhuanfa_num();

            $this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }


    //微博 待同步数据 堆积数量
    function sync_queue_num(){


        try {

            $current_num = $this->queue_model->get_task_yijian_num();

            $this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }


    //2018年01月17日11:25:17
    //微博 user_info 队列堆积数量
    function userinfo_queue_num(){

        try {

            $current_num = $this->queue_model->get_crawl_user_info_num();

            $this->success($current_num);
            return true;

        }catch (Exception $e){

            $this->error($e);

        }

    }


}
	
	
